window.chatsnapshotmessagedb = {
	insert: (msg, call) => {
		if (msg == null) {
			return;
		}
		let userId = app.getUserId();
		let message_id = msg.messageId == null ? -1 : msg.messageId;
		let create_time = msg.createTime;
		let message = msg.message;
		let status = msg.status;
		let receive_user_id = msg.receiveUserId;
		let send_user_id = msg.sendUserId;
		let type = msg.type;
		let is_myself = msg.isMyself;
		let unread = msg.unread;
		let friendId = send_user_id == userId ? receive_user_id : send_user_id;
		let sql = 'delete from t_chat_snapshot_message where 1=1 and user_id=' + userId + ' and friend_id=' + friendId;
		// console.log(JSON.stringify(sql));
		db.excuteSql(sql, (data) => {
			sql =
				'insert into t_chat_snapshot_message (message_id, create_time, message, status, receive_user_id, send_user_id, type, is_myself, unread, user_id, friend_id) ' +
				'values (' +
				message_id + ', "' + create_time + '", "' + message + '", ' + status + ', ' + receive_user_id + ', ' + send_user_id + ', ' + type +
				', ' + is_myself + ', ' + unread + ', ' + userId + ', ' + friendId +
				' )'
			db.excuteSql(sql, call);
		});
	},
	delete: (id, call) => {
		if (!id) {
			return;
		}
		let sql = 'delete from t_chat_snapshot_message where id = ' + id;
		db.excuteSql(sql, call);
	},
	deleteByMapping: (params, call) => {
		if (!params) {
			return;
		}
		let userId = params.userId;
		let friendId = params.friendId;
		if (userId == null || friendId == null) {
			return;
		}
		let sql = 'delete from t_chat_snapshot_message where 1=1 and user_id=' + userId + ' and friend_id=' + friendId;
		db.excuteSql(sql, call);
	},
	update: (msg, call) => {
		if (msg == null) {
			return;
		}
		let id = msg.id;
		let status = msg.status;
		let unread = msg.unread;
		let sql = 'update t_chat_snapshot_message set status=' + status + ', unread=' + unread + ' where id=' + id;
		db.excuteSql(sql, call);
	},
	updateUnread: (params, call) => {
		if (params == null) {
			return;
		}
		let id = params.id;
		let friendId = params.friendId;
		let unread = params.unread;
		let sql = 'update t_chat_snapshot_message set unread=' + unread + ' where 1=1 '
		if(id){
			sql += ' and id=' + id;
		} else if(friendId){
			sql += ' and send_user_id=' + friendId;
		}
		db.excuteSql(sql, call);
	},
	select: (params, call) => {
		let sql =
			' select t1.id, t1.create_time, t1.message, t1.status, t1.receive_user_id, t1.send_user_id, type, t1.is_myself, t1.unread, ' +
			' t1.user_id, t1.friend_id, t2.thum_head_portrait, t2.nickname, t2.account from t_chat_snapshot_message t1 ' +
			'join t_friend t2 on t2.friend_id=t1.friend_id and t2.user_id=t1.user_id' + 
			' where 1=1 ';
		if (params == null) {
			return [];
		}
		let userId = params.userId;
		if(params.id){
			sql += ' and t1.id=' + params.id;
		} else if (userId){
			sql += ' and t1.user_id=' + userId;
		}
		let friendId = params.friendId;
		if(friendId){
			sql += ' and t1.friend_id=' + friendId;
		}
		db.selectSQL(sql, call, params);
	}
}
